#include "ScriptManager.h"
#include "Script.h"

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

CScriptManager::CScriptManager(void)
{
}

CScriptManager::~CScriptManager(void)
{
}

CScriptManager* CScriptManager::GetInstance( void )
{
	static CScriptManager s_Instance;

	return &s_Instance;
}

void CScriptManager::InitializeLua( void )
{
	// Load a Lua state - This is basically loading Lua itself, it is EVERYTHING!
	L = lua_open();

	// Load in the libraries
	luaL_openlibs( L );
}

void CScriptManager::ShutdownLua( void )
{
	// TODO - clean data first
	//for( decltype( m_mScripts.size() ) i = 0; i < m_mScripts.size(); i++ )
	m_mScripts.clear();

	// Close Lua
	lua_close( L );
}

void CScriptManager::LoadLuaScript( const char* szFilename )
{
	// Variable to see if the file was loaded correctly
	int nError = 0;

	// Load the script
	nError = luaL_dofile( L, szFilename );

	if( nError != 0 )
	{
		MessageBoxA( 0, lua_tostring( L, -1 ), "Error!", MB_OK );
	}
}

void CScriptManager::Store( CScript* pScript, std::string pTag )
{
	// Store the script inside the "map" contained data member using the tag as an index
	m_mScripts[ pTag ] = pScript;
}